package com.zzy.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ListRepeat {
    public static void main(String[] args) {
        removeListString();
    }

    //String 去重复
    public static void removeListString(){
        List<String> list = new ArrayList<>();
        list.add("s1");
        list.add("s2");
        list.add("s1");
        System.out.print("去重前：");
        list.forEach(e->{
            System.out.print(e+" ");
        });
        Set<String> stringSet = new HashSet<>(list);
        List<String> list2 = new ArrayList<>(stringSet);
        System.out.println("");
        System.out.print("去重后1：");
        list2.forEach(e->{
            System.out.print(e+" ");
        });

        // 如果是jdk8 可以使用此方法更为简洁
        List<String> list3 =  list.stream().distinct().collect(Collectors.toList());
        System.out.println("");
        System.out.print("去重后2：");
        list3.forEach(e->{
            System.out.print(e+" ");
        });
    }

}
